using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//for use in finger touch control for world map
public class FingerGestureController : MonoBehaviour
{
    public Camera cam;
    public float left;
    public float right;
    public float top;
    public float down;
    public float near;
    public float far;

    public static FingerGestureController instance;

	// listen to input event;
	protected void Start ()
	{
        instance = this;	
	}
    
    void OnTap( TapGesture gesture )
    {
        Debug.Log(gesture.Selection);
    }
    
    void OnDoubleTap( TapGesture gesture )
    {
        Debug.Log("OnDoubleTap" + gesture.Selection + "position" + gesture.position);
    }

   
    void OnSwipe( SwipeGesture gesture )
    {
        Debug.Log("OnSwipe" + gesture.StartSelection + 
                  ",Direction:" + gesture.Direction + 
                  ",gesture.Velocity:" + gesture.Velocity + 
                  ",distance: " + gesture.Move.magnitude);
    }
    
    int dragFingerIndex = -1;
    void OnDrag( DragGesture gesture )
    {
        // first finger
        FingerGestures.Finger finger = gesture.Fingers[0];
        
        if( gesture.Phase == ContinuousGesturePhase.Started )
        {
            Debug.Log("OnDrag Started" + gesture.Selection + "position" + gesture.Position);
            // remember which finger is dragging dragObject
            dragFingerIndex = finger.Index;
        }
        else if( finger.Index == dragFingerIndex ) // gesture in progress, make sure that this event comes from the finger that is dragging our dragObject
        {
            if( gesture.Phase == ContinuousGesturePhase.Updated )
            {
                Debug.Log("OnDrag Updated" + gesture.Selection + "position" + gesture.Position);
            }
            else
            {
                dragFingerIndex = -1;// reset our drag finger index                   
            }
        }
    }
    
    public bool Rotating;
    void OnTwist( TwistGesture gesture )
    {
        if( gesture.Phase == ContinuousGesturePhase.Started)
        {
            Debug.Log("OnTwist" + gesture.Phase);
            Rotating = true;
        }
        else if( gesture.Phase == ContinuousGesturePhase.Updated )
        {
            if( Rotating )
            {
                Debug.Log("OnTwist" + gesture.Phase + "DeltaRotation:" + gesture.DeltaRotation);
            }
        }
        else
        {
            if( Rotating )
            {
                Debug.Log("OnTwist" + gesture.Phase + "TotalRotation:" + gesture.TotalRotation);
                Rotating = false;
            }
        }
    }
    
    public bool Pinching;
    public float pinchScaleFactor = 0.02f;
    void OnPinch( PinchGesture gesture )
    {
        if( gesture.Phase == ContinuousGesturePhase.Started )
        {
            Debug.Log("OnPinch" + gesture.Phase);
            Pinching = true;
        }
        else if( gesture.Phase == ContinuousGesturePhase.Updated )
        {
            if( Pinching )
            {
                Debug.Log("OnPinch" + gesture.Phase);
                // change the scale of the target based on the pinch delta value
                float z = Mathf.Clamp(gesture.Delta * pinchScaleFactor,far,near);
                cam.transform.position = new Vector3(cam.transform.position.x,cam.transform.position.y,z);
            }
        }
        else
        {
            if( Pinching )
            {
                Debug.Log("OnPinch" + gesture.Phase);
                Pinching = false;
            }
        }
    }
}


